# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：get_idCard.py
#日期：2019-03-11
#备注：生成虚拟身份证-->身份证组成：出生地6位+出生日期8位+顺序码2位+性别1位+校验码1位 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import pymysql
import time

from pycacho.cachobase.logger import Logger
from pycacho.cachobase.deal_idcard import get_idCard

logger = Logger("get_idCard").get_log()

db = pymysql.connect(
            host="10.16.20.182",
            user="dev",
            password="dev",
            port=3306,
            use_unicode=True,
            charset="utf8",
            database="qdzjmsloandb")

cursor = db.cursor()

regions_dict = {
    "650102":"新疆维吾尔自治区乌鲁木齐市天山区",
    "650104":"新疆维吾尔自治区乌鲁木齐市新市区",
    "650109":"新疆维吾尔自治区乌鲁木齐市米东区",
    "650106":"新疆维吾尔自治区乌鲁木齐市头屯河区"
}


def createTable():
    sql1 = 'drop table if exists zeng_idCardInfo ;'
    sql2 = 'create table zeng_idCardInfo (No int(11) AUTO_INCREMENT,id_region VARCHAR(20),id_regionInfo VARCHAR(20),id_birthday VARCHAR(20),id_card VARCHAR(20),PRIMARY KEY (No));'

    cursor.execute(sql1)
    cursor.execute(sql2)
    db.commit()


def get_card():
    # 总数量=regions个数*birthdays个数*800  4*10*800=32000+4*800
    regions = ['650102','650104','650109','650106']
    #birthdays = ['19881103','19881104','19881105','19881106','19881107','19881108','19881109','19881110','19881111','19881112']
    birthdays = ['19690925']
    idCards = get_idCard(regions,birthdays,100)
    print("生成身份证个数为：", len(idCards))
    for card in idCards:
        print(card[0:6],regions_dict[card[0:6]],card[6:14],card)
        idCard_to_mysql(card[0:6],regions_dict[card[0:6]],card[6:14],card)
    return(idCards)


def idCard_to_mysql(id_region,id_regionInfo,id_birthday,id_card):
    sql = "insert into zeng_idCardInfo (id_region,id_regionInfo,id_birthday,id_card) values ('%s','%s','%s','%s');"\
          % (id_region,id_regionInfo,id_birthday,id_card)
    try:
        cursor.execute(sql)
    except Exception as e:
        # 发生错误时回滚
        db.rollback()
        print(str(e))
    else:
        db.commit()  # 事务提交
        logger.info('%s身份证信息插入成功！', id_card)


if __name__ == '__main__':
    cTime = time.time()
    #createTable()
    get_card()
    logger.info("总共耗时：%s ", time.time() - cTime)